{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 模块\n",
    "- 一个模块就是一个python文件\n",
    "- 好处\n",
    "\t- 程序太大，维护不方便，需要拆分\n",
    "\t- 模块可以增加代码的复用性\n",
    "\t- 当作命令空间使用，避免命名冲突\n",
    "- 模块就是一系列代码的集合，但是尽量包括 class 和  function\n",
    "- 语法 ：导入就相当于把代码全部复制到该页面\n",
    "\t- import module_name as 别名\n",
    "\t- from module import class_name,function_name 部分导入 ：这样可以省略模块名\n",
    "\t- from module import * 导入所有内容，但是用的时候可以省略模块名\n",
    "\t- module_name.class_name()\n",
    "\t- module.name.function_name()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是文件主线程\n"
     ]
    }
   ],
   "source": [
    "# 只有当该文件是主文件时，才会执行\n",
    "if __name__ == '__main__':\n",
    "    print('这是文件主线程')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模块路径问题：关于py程序怎么找到import内容的问题\n",
    "- 获取所有可寻找的路径\n",
    "\t- import sys      print(sys.path)\n",
    "\t- 增加一个路径到可查找路径  sys.path.append(\"路径地址\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 包：模块的集合\n",
    "- 组成：\n",
    "\t- `__init__.py`文件，这是必须的\n",
    "\t- 任意个py文件\n",
    "\t- 子包\n",
    "- 导入方法\n",
    "\t- import packet_name : 这样的话只是导入了`__init__.py`模块的内容\n",
    "\t- import packet_name.mudule_name : 导入了指定模块\n",
    "\t- from packet_name import mudule.name : 导入指定模块\n",
    "\t- from packet_name import * :\n",
    "\t\t- 如果`__init__.py`为空或者不存在或者没有'__all__`,那么导入的就是`__init__.py`\n",
    "\t\t- 如果有的话，那么导入的是'__all__`中的模块内容\n",
    "-  `__all__`=['module1,mudule2,...]:在`__init__.py`开头定义就可以了"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
